package com.engine4cloud.tdf.supervision.controller;

import com.engine4cloud.tdf.common.core.constant.enums.DictNameEnum;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.supervision.dto.query.DictChildrenListQueryDTO;
import com.engine4cloud.tdf.supervision.dto.query.DictCodeListQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.DictDescribeResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.DictSelectResultDTO;
import com.engine4cloud.tdf.supervision.service.DictSelectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典查询-三端
 *
 * @author pig
 * @date 2024-02-29 11:13:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dict")
@Tag(description = "dict", name = "字典查询-三端")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DictSelectController {

    @Resource
    private DictSelectService dictSelectService;

    /**
     * 全部字典-名称列表
     */
    @Operation(summary = "全部字典-名称列表", description = "全部字典-名称列表")
    @GetMapping("/all")
    public R<List<DictDescribeResultDTO>> dictionaries() {
        List<DictDescribeResultDTO> labelList = Arrays.stream(DictNameEnum.values())
                .map(state -> new DictDescribeResultDTO(state.getType(), state.getDesc(), state.getRemark()))
                .collect(Collectors.toList());
        return R.ok(labelList);
    }

    /**
     * 单个字典-值列表
     */
    @Operation(summary = "单个字典-值列表", description = "单个字典-值列表")
    @GetMapping("/items/{keywords}")
    public R<List<DictSelectResultDTO>> items(@PathVariable("keywords") String keywords) {
        List<DictSelectResultDTO> items = dictSelectService.items(keywords);
        return R.ok(items);
    }

    /**
     * 父子结构的字典-第一层子集列表
     *
     * @param dto dto
     * @return List<DictSelectResultDTO>
     */
    @Operation(summary = "父子结构的字典-第一层子集列表", description = "父子结构的字典-第一层子集列表")
    @PostMapping("/children")
    public R<List<DictSelectResultDTO>> firstFloorChildren(@Valid @RequestBody DictChildrenListQueryDTO dto) {
        List<DictSelectResultDTO> items = dictSelectService.firstFloorChildren(dto);
        return R.ok(items);
    }

    /**
     * 字典获取-列表
     */
    @Operation(summary = "字典获取-列表", description = "字典获取-列表")
    @PostMapping("/items")
    public R<List<DictSelectResultDTO>> items(@Valid @RequestBody DictCodeListQueryDTO dto) {
        List<DictSelectResultDTO> items = dictSelectService.items(dto);
        return R.ok(items);
    }

}